<?php

    /**
     * Installer validation
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */

    set_time_limit(0);
    ini_set('max_execution_time', 0);

    require '../app/Main.php';

    Main::setIncludes();

    $installer = null;

    try {
        $installer = Core_Installer::getInstance();
    } catch (Core_Exception $e) {
        die(Zend_Json::encode(array(
                                    'success'   => false,
                                    'errors'    => array(
                                                            'reason' => $e->getMessage()
                                                         )
                                   )
                              ));
    }

    try {
        $installer->check($_POST);
    } catch (Core_Installer_Exception $e) {
        die(Zend_Json::encode(array(
                                    'success'   => false,
                                    'errors'    => array(
                                                            'fields' => $e->getErrors()
                                                         )
                                   )
                              ));

    } catch (Core_Exception $e) {
        die(Zend_Json::encode(array(
                                    'success'   => false,
                                    'errors'    => array(
                                                            'reason' => $e->getMessage()
                                                         )
                                   )
                              ));
    }

    try {
        $basePath = $installer->process();

        echo Zend_Json::encode(array(
                                     'success'   => true,
                                     'page'      => $basePath .'serum/index.php?generate=true&data=true&email='.$_POST['fieldEmail']
                                     )
                                );

    } catch (Core_Exception $e) {
        die(Zend_Json::encode(array(
                                    'success'   => false,
                                    'errors'    => array(
                                                            'reason' => $e->getMessage()
                                                         )
                                   )
                              ));
    }
